const express = require('express');
const db = require('../utils/db.js');

const router = express.Router();

router.post('/selectuserbalance', (req, res) => {
  const sql = `SELECT balance FROM userinfo WHERE id='${req.body.userid}';`
  db.query(sql, (err, data) => {
    if (err) {
      res.send({
        msg: err
      })
    } else {
      res.send(data)
    }
  })
})

router.post('/editbalance', (req, res) => {
  const sql = `UPDATE userinfo set balance='${req.body.balance}' where id='${req.body.userid}';`
  db.query(sql, (err, data) => {
    if (err) {
      res.send({
        msg: err
      })
    } else {
      res.send({
        msg: '支付成功'
      })
    }
  })
})

router.get('/addorderlist', (req, res) => {
  const inserSql =
    `INSERT INTO orderlist VALUES(null,${req.query.type_id},'${req.query.status}',${req.query.peopleNum},null,'${req.query.contacphone}','${req.query.price}',${req.query.user_id},${req.query.shop_ID},null,'${req.query.num}','${req.query.table_num}','${req.query.paymethod}','${req.query.remark}',${req.query.total},null,${req.query.discount},null,null,null,null)`;
  // console.log(req.query.peopleNum)
  db.query(inserSql, (err, data) => {
    if (err == null) { //插入成功
      // console.log('111:', data);
      res.send(data)
    } else {
      res.send({
        msg: err
      })
    }
  })
})

router.get('/addmenu', (req, res) => {
  const inserSql = `INSERT INTO ordermenu VALUES(null,${req.query.menuid},${req.query.orderid},null,1,1)`;
  // console.log(inserSql);
  db.query(inserSql, (err, data) => {
    if (err == null) { //插入成功
      res.send(data)
    } else {
      res.send({
        msg: err
      })
    }
  })
})

// router.post('/sel',(req,res)=>{
// 	const sql=`SELECT  max(id)  FROM  orderlist;`
// 	db.query(sql,(err,data)=>{
// 		if(err){
// 			res.send({msg:err})
// 		}else{
// 			res.send(data)
// 		}
// 	})
// })

module.exports = router;
